<?php

/**
 * Created by PhpStorm.
 * User: 张彪
 * Date: 2016/5/6 0006
 * Time: 上午 10:12
 */
class Ziying_model extends Base_Model
{
    public function __construct()
    {
        $this->load->database();
    }


    public function getList($data = array())
    {

        if(!empty($data['limit']))
        {
            $offset = empty($data['offset'])?0:$data['offset'];
            $this->db->limit($offset, $data['limit']);
        }

        if(!empty($data['city']))
        {
            $citys = explode(',',$data['city']);
            $this->db->where_in('city.cityId', $citys);
        }
//        $this->db->join('  c', 'z.id = c.track_id', 'left');
        $this->db->select('	z.*,c.count');
        $this->db->join('mod_ziying_city  as city','z.id = city.zyId','left');
        //获取总条数
        $this->db->join('(SELECT
                            COUNT(*) as count,
                            track_id
                        FROM
                            mod_track_comments
                        GROUP BY
                            track_id ) as c', 'z.id = c.track_id', 'left');
        $this->db->from('mod_ziying as z');
        $query = $this->db->get()->result_array();
        $items = array();
        if(!empty($query))
        {
            foreach($query as $v)
            {
                $item = array();
                $item['id'] = $v['id'];
                $item['name'] = $v['name'];
                $item['thumb'] = $this->img($v['thumb']);
                $item['orders'] = $v['orders'];
                $item['city'] =  $this->getCityBy($v['id']);
                $item['summary'] = $v['summary'];
                $item['priceTotal'] = $v['priceTotal'];
                $item['count'] = empty($v['count'])?0:$v['count'];
                $items[] = $item;
            }

        }
        return $items;
    }


    public function getCityBy($id)
    {
        $this->db->where('z.zyId', $id);
        $this->db->join('mod_city as city', 'city.id = z.cityId', 'left');
        $this->db->from('mod_ziying_city as z');
        $query = $this->db->get()->result_array();
        $city = array();
        foreach ($query as $k => $v) {
            $c = explode('|', $v['cityName']);
            $city[] = $c[false];
        }

        if (!empty($city))
        {
            return implode('|',$city);
        }else{
            return '';
        }

    }

    public function detail($id)
    {
        $this->db->where('id', $id);
        $this->db->from('mod_ziying');
        $query = $this->db->get()->row_array();
        if(!empty($query['uid']))
        {
            $guider = $this->getUserInfoById($query['uid']);
        }else{
            return false;
        }
        $Imgs = explode('|',$query['images']);
        if(!empty($Imgs))
        {
            foreach ($Imgs as $k=>$img)
            {
                $Img[$k] = $this->img($img);
            }

        }

        $item['name'] = $query['name'];
        $item['userIcon'] = $this->img($guider['userIcon']);
        $item['realName'] = $guider['realName'];
        $item['identityActive'] = $guider['identityActive'];
        $item['telActive'] = $guider['telActive'];
        $item['resideCity'] = $guider['resideCity'];
        $item['resideTime'] = $guider['resideTime'];
        $item['userAge'] = $guider['userAge'];
        $item['userGender'] = $guider['userGender'];
        $item['identityName'] = $guider['identityName'];
        $item['identityErea_zhuanye'] = $guider['identityErea_zhuanye'];
        $item['identityErea_school'] = $guider['identityErea_school'];
        $item['introduction'] = nl2br($guider['introduction']);
        $item['serviceMaxPersons'] = $query['serviceMaxPersons'];
        $item['summary'] = nl2br($query['summary']);
        $item['images'] = $Imgs;
        $item['zan'] = $query['zan'];
        $item['price'] = round($query['priceTotal'] / 100, 2) . '€';
        $item['days'] = $query['days'];
        $item['orders'] = $query['orders'];
        $item['recommend'] = $query['recommend'];
        $item['provider'] = $query['provider'];
        $item['providerIcon'] = $this->img($query['providerIcon']);
        $item['descLineApp'] = $query['descLineApp'];
        $item['descPriceApp'] = $query['descPriceApp'];
        return $item;

    }
   public function getbyid($id)
   {
       $this->db->where('id', $id);
       $this->db->from('mod_ziying');
       $query = $this->db->get()->row_array();
       return $query;
   }

}